/**
 * MACAddressValidator.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2015年5月18日
 */
package com.pshare.framework.validator;

import java.util.regex.Pattern;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang3.StringUtils;

import com.pshare.constant.ReturnMsg;
import com.pshare.framework.annotation.MACAddressCheck;

/**
 * 电话号码验证
 */
public class MACAddressValidator implements ConstraintValidator<MACAddressCheck, CharSequence> {

	private static String REGEX1 = "[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]:[0-9A-F][0-9A-F]";
	private static String REGEX2 = "[0-9A-F]{12}";

	private static Pattern pattern1 = Pattern.compile(REGEX1);
	private static Pattern pattern2 = Pattern.compile(REGEX2);

	@Override
	public void initialize(MACAddressCheck constraintAnnotation) {
		
	}

	@Override
	public boolean isValid(CharSequence object, ConstraintValidatorContext constraintContext) {
		if (StringUtils.isBlank(object ))
			return false;

		String input = object.toString().trim();
		boolean valid = false;
		if (pattern1.matcher(input).matches() || pattern2.matcher(input).matches()) {
			valid = true;
		}

        if(!valid) {
            constraintContext.disableDefaultConstraintViolation();
            constraintContext.buildConstraintViolationWithTemplate(ReturnMsg.MAC_FORMAT).addConstraintViolation();
        }
        
        return valid;
	}

}
